﻿CREATE PROCEDURE [dbo].[yaf_forum_delete](
                @ForumID INT)
AS
    BEGIN
        -- Maybe an idea to use cascading foreign keys instead? Too bad they don't work on MS SQL 7.0...
        UPDATE yaf_Forum
        SET    LastMessageID = NULL,
               LastTopicID = NULL
        WHERE  ForumID = @ForumID
        UPDATE yaf_Topic
        SET    LastMessageID = NULL
        WHERE  ForumID = @ForumID
        DELETE FROM yaf_WatchTopic
        FROM        yaf_Topic
        WHERE       yaf_Topic.ForumID = @ForumID
        AND yaf_WatchTopic.TopicID = yaf_Topic.TopicID
        DELETE FROM yaf_Active
        FROM        yaf_Topic
        WHERE       yaf_Topic.ForumID = @ForumID
        AND yaf_Active.TopicID = yaf_Topic.TopicID
        DELETE FROM yaf_NntpTopic
        FROM        yaf_NntpForum
        WHERE       yaf_NntpForum.ForumID = @ForumID
        AND yaf_NntpTopic.NntpForumID = yaf_NntpForum.NntpForumID
        DELETE FROM yaf_NntpForum
        WHERE       ForumID = @ForumID
        DELETE FROM yaf_WatchForum
        WHERE       ForumID = @ForumID
        -- BAI CHANGED 02.02.2004
        -- Delete topics, messages and attachments
        DECLARE  @tmpTopicID INT;
        DECLARE topic_cursor CURSOR  FOR
        SELECT   TopicID
        FROM     yaf_Topic
        WHERE    ForumID = @ForumID
        ORDER BY TopicID DESC
        OPEN topic_cursor
        FETCH NEXT FROM topic_cursor
        INTO @tmpTopicID
        -- Check @@FETCH_STATUS to see if there are any more rows to fetch.
        WHILE @@FETCH_STATUS = 0
        BEGIN
            EXEC yaf_topic_delete
                 @tmpTopicID ,
                 1 ,
                 1;
            -- This is executed as long as the previous fetch succeeds.
            FETCH NEXT FROM topic_cursor
            INTO @tmpTopicID
        END
        CLOSE topic_cursor
        DEALLOCATE topic_cursor
        -- TopicDelete finished
        -- END BAI CHANGED 02.02.2004
        DELETE FROM yaf_ForumAccess
        WHERE       ForumID = @ForumID
        --ABOT CHANGED
        --Delete UserForums Too
        DELETE FROM yaf_UserForum
        WHERE       ForumID = @ForumID
        --END ABOT CHANGED 09.04.2004
        DELETE FROM yaf_Forum
        WHERE       ForumID = @ForumID
    END


